package mysql

import (
	"video-up-manager/videorpc/videorpc"

	"gorm.io/gorm"
)

type Video struct {
	gorm.Model
	UploadUserId  int64  `gorm:"type:int(11);not null;comment:'上传用户ID'"`
	VideoTitle    string `gorm:"type:varchar(30);not null;comment:'视频标题'"`
	VideoCategory string `gorm:"type:varchar(30);not null;comment:'视频分类'"`
	VideLink      string `gorm:"type:varchar(300);not null;comment:'视频URL'"`
	CoverLink     string `gorm:"type:varchar(300);not null;comment:'视频封面URL'"`
	VideoIntro    string `gorm:"type:varchar(500);comment:'视频简介'"`
	PlayNum       int64  `gorm:"type:int(11);default:0;comment:'播放量'"`
	LikeNum       int64  `gorm:"type:int(11);default:0;comment:'点赞数'"`
	CollectNum    int64  `gorm:"type:int(11);default:0;comment:'收藏数'"`
	CommentNum    int64  `gorm:"type:int(11);default:0;comment:'评论数'"`
	ReleaseTime   string `gorm:"type:varchar(50);comment:'发布时间'"`
	VideoStatus   string `gorm:"type:enum('1','0','2');default:'1';comment:'视频状态'"`
	TopicId       int64  `gorm:"type:int(11);comment:'关联话题ID'"`
	VideoDuration string `gorm:"type:varchar(50);not null;comment:'视频时长'"`
}

func (v *Video) VideoAdd(db *gorm.DB) error {
	return db.Debug().Create(v).Error

}

func (v *Video) FindVideoByTitle(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(v).Error
}

func (v *Video) VideoUpdate(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Updates(v).Error
}

func (v *Video) VideoDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(v).Error
}

func (v *Video) VideoShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Find(v).Error
}

func (v *Video) VideoList(db *gorm.DB, id int64, in *videorpc.VideoListRequest, lists []*videorpc.VideoList) ([]*videorpc.VideoList, error) {
	/*
		SELECT
		  videos.id as video_id,users.nick_name,videos.video_title,videos.video_category,videos.vide_link,videos.cover_link,videos.video_intro,videos.play_num,videos.like_num,videos.collect_num,videos.comment_num,videos.release_time,videos.video_status,topics.topic_name,videos.video_duration
		FROM
		  videos
		  LEFT JOIN users ON users.id = videos.upload_user_id
		  LEFT JOIN topics ON topics.id = videos.topic_id
		  ORDER BY videos.release_time DESC
	*/
	err := db.Debug().Model(v).
		Select(" videos.id as video_id,users.nick_name,videos.video_title,videos.video_category,videos.vide_link,videos.cover_link,videos.video_intro,videos.play_num,videos.like_num,videos.collect_num,videos.comment_num,videos.release_time,videos.video_status,topics.topic_name,videos.video_duration").
		Joins("LEFT JOIN users ON users.id = videos.upload_user_id").
		Joins(" LEFT JOIN topics ON topics.id = videos.topic_id").
		Where("users.id=?", id).
		Order("videos.release_time desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error
	return lists, err
}
